import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../provider/userprovider.dart';
import 'navigate_ui.dart';
import 'login_page.dart';

class RootPage extends StatefulWidget {
  const RootPage({Key? key}) : super(key: key);

  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  /// Views to display
  List<Widget> views = const [LoginScreen(), NavigateUi()];
  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<UserProvider>(
      builder: (context, userState, child) {
        userState.addListener(() {
          if (userState.user.authed) {
            setState(() {
              selectedIndex = 1;
            });
          } else {
            setState(() {
              selectedIndex = 0;
            });
          }
        });

        return views[selectedIndex];
      },
    );
  }
}
